package com.xingxing.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.recyclerview.widget.RecyclerView;
import com.xingxing.Activity.EditProfileActivity;
import com.xingxing.Activity.R;
import com.xingxing.Activity.UseDeviceActivity;
import com.xingxing.Bean.Device;
import com.xuexiang.xui.utils.XToastUtils;

import java.util.List;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.MyViewHolder> {
    List<Device> device;
    private Context mContext;

    public DeviceAdapter(Context context, List<Device> device) {
        mContext = context;
        this.device = device;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item_home, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.deviceId.setText(device.get(position).getDeviceId());
        holder.location.setText(device.get(position).getLocation());
        holder.status.setText(device.get(position).getStatus());
        if (holder.status.getText().toString().equals("离线")) {
            holder.icon.setImageResource(R.drawable.ic_device_disconnect);
            holder.statusIcon.setImageResource(R.color.color_gray_6);
        }
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (device.get(position).getStatus().equals("离线")) {
                    XToastUtils.toast("设备离线");
                } else {
                    Intent intent = new Intent(mContext, UseDeviceActivity.class);
                    intent.putExtra("deviceId", device.get(position).getDeviceId());
                    intent.putExtra("location", device.get(position).getLocation());
                    mContext.startActivity(intent);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return device.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView deviceId;
        TextView location;
        TextView status;
        com.xuexiang.xui.widget.imageview.RadiusImageView icon;
        com.xuexiang.xui.widget.imageview.RadiusImageView item;
        com.xuexiang.xui.widget.imageview.RadiusImageView statusIcon;

        public MyViewHolder(View view) {
            super(view);
            deviceId = (TextView) view.findViewById(R.id.device_id_value);
            location = (TextView) view.findViewById(R.id.location_value);
            status = (TextView) view.findViewById(R.id.status_value);
            icon = (com.xuexiang.xui.widget.imageview.RadiusImageView) view.findViewById(R.id.icon);
            item = (com.xuexiang.xui.widget.imageview.RadiusImageView) view.findViewById(R.id.round_background);
            statusIcon = (com.xuexiang.xui.widget.imageview.RadiusImageView) view.findViewById(R.id.ic_status);
        }
    }


}